package com.etrans.web.gps.util;

import java.net.URL;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class GetAdd {
	
	public static String[] getAddArray(String log, String lat){
		//默认地址 
//		String defaultAddress = getAdd("116.3039", "39.97646");
		String defaultAddress = "{\"queryLocation\":[39.97646,116.3039],\"addrList\":[{\"type\":\"poi\",\"status\":1,\"name\":\"海淀南路31号院1号楼\",\"id\":\"ANB000A9UZ4E\",\"admCode\":\"110108\",\"admName\":\"北京市,北京市,海淀区,\",\"addr\":\"\",\"nearestPoint\":[116.30395,39.97609],\"distance\":37.336}]}";
		
		String add ="";
		if(("".equals(log)||"".equals(lat))||(log==null||lat==null)||("null".equalsIgnoreCase(log)||"null".equalsIgnoreCase(lat))){
			add = defaultAddress; 
		}else{
			add=getAdd(log,lat);
		}
		if(add != null && !"".equals(add)){
			try {
				if(add.contains("\\")){
					System.out.println("===add:===="+add);
					//昆明长水国际机场(国际\港澳台到达)
					add = add.replaceAll("\\\\", "");
				}
				JSONObject jsonObject = JSONObject.parseObject(add);     
				JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));  
				JSONObject j_2 = JSONObject.parseObject(String.valueOf(jsonArray.get(0)));  
				String allAdd = j_2.getString("admName");  
				String arr[] = allAdd.split(","); 
				//System.out.println("省："+arr[0]+"\n市："+arr[1]+"\n区："+arr[2]); 
				return arr;
			} catch (Exception e) {
				System.out.println("===add:===="+add);
				e.printStackTrace();
				String[] defaultArr={"北京","北京市"};
				return defaultArr;
			}
		}else{
			String[] defaultArr={"北京","北京市"};
			return defaultArr;
		} 
	}

	public static String getAddProvince(String log, String lat){
		//默认地址
//		String defaultAddress = getAdd("116.3039", "39.97646");

		String add ="";
		if((!"".equals(log)&&!"".equals(lat))&&log!=null&&lat!=null){
			add=getAdd(log,lat);
		}

		JSONObject jsonObject = JSONObject.parseObject(add);
		JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));
		JSONObject j_2 = JSONObject.parseObject(String.valueOf(jsonArray.get(0)));
		String allAdd = j_2.getString("admName");
		String arr[] = allAdd.split(",");
		//System.out.println("省："+arr[0]+"\n市："+arr[1]+"\n区："+arr[2]);
		return arr[0];
	}
	
	 public static String getAdd(String log, String lat ){  
	        //lat 小  log  大  
	        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)  
	        String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";
	        String res = "";     
	        try {     
	            URL url = new URL(urlString);    
	            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
	            conn.setDoOutput(true);    
	            conn.setRequestMethod("POST");    
	            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));    
	            String line;    
	           while ((line = in.readLine()) != null) {    
	               res += line+"\n";    
	         }    
	            in.close();    
	        } catch (Exception e) {    
	            System.out.println("error in wapaction,and e is " + e.getMessage());  
	            e.printStackTrace();
	        }   
	        //System.out.println(res);  
	        return res;    
	    }
	public static String getAddcounty (String log, String lat){
		//默认地址
//			String defaultAddress = getAdd("116.3039", "39.97646");

		String add ="";
		if((!"".equals(log)&&!"".equals(lat))&&log!=null&&lat!=null){
			add=getAdd(log,lat);
		}

		JSONObject jsonObject = JSONObject.parseObject(add);
		JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));
		JSONObject j_2 = JSONObject.parseObject(String.valueOf(jsonArray.get(0)));
		String allAdd = j_2.getString("admName");
		String arr[] = allAdd.split(",");
		//System.out.println("省："+arr[0]+"\n市："+arr[1]+"\n区："+arr[2]);
		return arr[2];
	}

	public static void main(String[] args) throws Exception {
		//默认地址
//		 String defaultAddress = getAddProvince("116.3039", "39.97646");
		 String defaultAddress = getAddcounty("108.960512", "25.85312");
		 System.out.println(defaultAddress);
		 
	}
}
